<?php

class Employee Extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->view_data['money'] = function($x){return number_format($x,2,'.',',');};
		$this->load->library(array('url_auth','form_validation'));
	}
	
	public function index()
	{

	}
	
	public function add()
	{
		$this->page_title = 'Add Employee';
		
		$this->view_data['auth'] = $this->url_auth->get_auth();
		$this->load->helper('form');
		$this->load->model('M_department','md');
		$this->view_data['system_message'] = $this->_msg();
		$this->view_data['gender_opt'] = array(''=>'- choose Gender -','m'=>'Male','f'=>'female');
		$this->view_data['status_opt'] = array(''=>'- choose status -','married'=>'married','single'=>'single');
		$this->view_data['department_opt'] = $this->md->get_department_array();
		
		if($this->input->post('add_employee'))
		{
			$this->form_validation->set_rules('firstname','FirstName','required|trim|htmlspecialchars');
			$this->form_validation->set_rules('lastname','Lastname','required|trim|htmlspecialchars');
			$this->form_validation->set_rules('middlename','Middlename','required|trim|htmlspecialchars');
			$this->form_validation->set_rules('gender','Gender','required|trim|htmlspecialchars');
			$this->form_validation->set_rules('address','Address','required|trim|htmlspecialchars');
			$this->form_validation->set_rules('status','marital Status','required|trim|htmlspecialchars');
			$this->form_validation->set_rules('dependents','dependents','required|trim|htmlspecialchars');
			$this->form_validation->set_rules('monthly_salary','Monthly Salary','required|numeric|trim|htmlspecialchars');
			$this->form_validation->set_rules('position','Position','trim|htmlspecialchars');
			$this->form_validation->set_rules('department','Department ','required|trim|htmlspecialchars');
		
			if($this->form_validation->run() !== FALSE)
			{
				$this->load->model('M_employee','me');
				if($this->me->new_employee($this->input->post()))
				{
					$this->_msg('s','Employee Successfully Added','employee/add');
				}else{
					$this->view_data['system_message'] = $this->_msg('e','An error was encountered while adding data');
				}
			}
		}
	}
	
	public function view()
	{
		$this->load->library('pagination');
		$this->load->model('M_employee','me');
		$this->view_data['system_message'] = $this->_msg();
		$this->view_data['all_employee'] = $this->me->all_employee();
	}
	
	public function update($id = '')
	{
		$this->load->library('token');
		$this->load->helper('form');
		$this->load->model('M_department','md');
		$this->view_data['auth'] = $this->url_auth->get_auth();
		$this->view_data['system_message'] = $this->_msg();
		$this->view_data['form_token'] = $this->token->get_token();
		$this->view_data['gender_opt'] = array(''=>'- choose Gender -','m'=>'Male','f'=>'female');
		$this->view_data['status_opt'] = array(''=>'- choose status -','married'=>'married','single'=>'single');
		$this->view_data['department_opt'] = $this->md->get_department_array();
		$this->view_data['role_opt'] = array_map('ucwords',array(''=>'-- choose role --',
																 'cashier'=>'cashier',
																 'inventory'=>'inventory',
																 'hrd'=>'Human Resource',
																 'accounting'=>'accounting',
																 'engineering'=>'engineering',
																 'housekeeping'=>'house keeping',
																 'frontoffice'=>'front office',
																 'restaurantpos'=>'restaurant point of sale'));
		if(is_numeric($id))
		{
			$this->load->model('M_employee','me');
			$this->view_data['emp'] = $this->me->one_employee($id);
			$this->view_data['employee_id'] = $id;
			
			if($this->input->post('update_employee'))
			{
				$this->form_validation->set_rules('firstname','FirstName','required|trim|htmlspecialchars');
				$this->form_validation->set_rules('lastname','Lastname','required|trim|htmlspecialchars');
				$this->form_validation->set_rules('middlename','Middlename','required|trim|htmlspecialchars');
				$this->form_validation->set_rules('gender','Gender','required|trim|htmlspecialchars');
				$this->form_validation->set_rules('address','Address','required|trim|htmlspecialchars');
				$this->form_validation->set_rules('status','marital Status','required|trim|htmlspecialchars');
				$this->form_validation->set_rules('dependents','dependents','required|trim|htmlspecialchars');
				$this->form_validation->set_rules('monthly_salary','Monthly Salary','required|numeric|trim|htmlspecialchars');
				$this->form_validation->set_rules('position','Position','trim|htmlspecialchars');
				
				if($this->form_validation->run() !== FALSE)
				{
					$this->load->model('M_employee','me');
					if($this->me->update_employee($this->input->post(),$id))
					{
						$this->_msg('s','Update was successfull','employee/view');
					}else{
						$this->view_data['system_message'] = $this->_msg('e','Update has encountered an error!.');
					}
				}
			}
			
			
			if($this->input->post('createlogin'))
			{
				if(is_numeric($this->input->post('employee_id')) AND $this->token->validate_token($this->input->post('form_token')))
				{
					$this->form_validation->set_rules('username','Username','required|trim|htmlspecialchars');
					$this->form_validation->set_rules('password','Password','required|trim|htmlspecialchars');
					$this->form_validation->set_rules('cpassword','Confirm Password','required|htmlspecialchars|trim|matches[password]');
					$this->form_validation->set_rules('role','User Role','required|trim|htmlspecialchars');
					
					if($this->form_validation->run() !== FALSE)
					{
						$this->load->model('M_employee','me');
						if($this->me->check_login($this->input->post(),$id))
						{
							$this->view_data['system_message'] = $this->_msg('s','Login data has been added successfully','employee/update/'.$id);
						}else{
							$this->view_data['system_message'] = $this->_msg('e','Unable to add Login Data');
						}
					}
				}
			}
			
			if($this->input->post('updatedepartmentandrole'))
			{
				if(is_numeric($this->input->post('employee_id')) AND $this->token->validate_token($this->input->post('form_token')))
				{
					$this->form_validation->set_rules('role','User Role','required|trim|htmlspecialchars');
					$this->form_validation->set_rules('department','Department','required|trim|htmlspecialchars');
					
					if($this->form_validation->run() !== FALSE)
					{
						$this->load->model('M_employee','me');
						if($this->me->update_role_dept($this->input->post(),$id))
						{
							$this->view_data['system_message'] = $this->_msg('s','Department and Role Updated','employee/update/'.$id);
						}else{
							$this->view_data['system_message'] = $this->_msg('e','Unable to add Login Data');
						}
					}
				}
			}
			
			if($this->input->post('delete_employee_data'))
			{
				if($this->input->post('employee_id') == $id)
				{
					$this->load->model('M_employee','me');
					
					if($this->me->delete_employee_data($this->input->post('employee_id')) == TRUE)
					{
						$this->_msg('s','Employee Deleted From our Records','employee/view');
					}else{
						$this->_msg('e','Unable to delete employee from database','employee/update/'.$id);
					}
				}
			}
			
			
			
			
		}
	}
	
	public function attendance()
	{
		$this->load->helper('form');
		$this->load->model('M_employee','me');
		$this->view_data['system_message'] = $this->_msg();
		$this->view_data['employee'] = $this->me->get_employee_for_attendance();
		$this->view_data['n_employee'] = $this->me->get_nemployee_for_attendance();
	
		if($this->input->post('add_attendance'))
		{
			if(strtolower($this->input->post('mode')) == 'no_attendance')
			{
				if($this->me->insert_attendance($this->input->post('attendance')))
				{
					$this->_msg('s','Attendance was saved','employee/attendance');
				}else{
					$this->view_data['system_message'] = $this->_msg('e','An error was encountered while processing request, Please Try Again');
				}
			}elseif(strtolower($this->input->post('mode')) == 'has_attendance'){
				if($this->me->update_attendance($this->input->post('attendance')))
				{
					$this->_msg('s','Attendance was saved','employee/attendance');
				}else{
					$this->view_data['system_message'] = $this->_msg('e','An error was encountered while processing request, Please Try Again');
				}
			}
		}
	}
	
	public function add_nattendance()
	{
		if($this->input->post('add_nattendance'))
		{
			$data = $this->input->post('new_employees',TRUE);
			$this->load->model('M_employee','me');
			
			if(!empty($data))
			{
				foreach($data as $k=> $data)
				{
					$input[$k]['emp_id'] = $data['emp_id'];
					$input[$k]['date'] = DATE;
				}
				
				if($this->me->insert_attendance($input))
				{	
					$this->_msg('s','Employees Successfully added to DTR','employee/attendance');
				}else{
					$this->_msg('e','An error was encountered while adding employees to DTR','employee/attendance');
				}
				
			}else{
				$this->_msg('e','Unable to add Employees','employee/attendance');
			}
		}else{
			redirect('employee/attendance');
		}
	}
	
	public function archive()
	{
		$this->view_data['system_message']= $this->_msg();
		$this->load->library(array('form_validation'));
		$this->load->helper(array('form'));
		
		
		if($this->input->post('get_archives'))
		{
			$this->form_validation->set_rules('date','Date','required|htmlspecialchars|trim|callback__check_date');
		
			if($this->form_validation->run() !== FALSE)
			{
				$this->load->model('M_employee','me');
				$this->view_data['schedule'] = $this->me->get_schedule($this->input->post('date'));
			}else{
				$this->view_data['system_message']= validation_errors() == '' ? NULL : $this->_msg('e',validation_errors());
			}
		}
	}
	
	public function intervals()
	{
		$this->load->library(array('form_validation'));
		$this->load->helper(array('form'));
		$this->page_title = 'DTR Intervals';
		$this->view_data['system_message'] = $this->_msg();
		
		
		if($this->input->post('check_date_intervals'))
		{
			$this->form_validation->set_rules('date_from','Date From','required|htmlspecialchars|trim|callback__check_date|callback__check_date_intervals');
			$this->form_validation->set_rules('date_to','Date To','required|htmlspecialchars|trim|callback__check_date');
			$this->form_validation->set_message('_check_date_intervals','Date To must not be lesser than date from');
			
			
			if($this->form_validation->run() !== FALSE)
			{
				$from = $this->input->post('date_from');
				$to = $this->input->post('date_to');
				$this->load->model('M_employee','me');
				$this->view_data['time_sched_intervals'] = $this->me->get_dtr_by_interval($from,$to);
			}else{
				$this->view_data['system_message'] = validation_errors() == '' ? NULL : $this->_msg('e',validation_errors());
			}
		}
	}

	public function _check_date_intervals($x)
	{
		$date_to = $this->input->post('date_to');
		$date_from = $x;
		
		
		if($this->_check_date($date_to) AND $this->_check_date($x))
		{
			$stamp_date_from = strtotime($date_from);
			$stamp_date_to = strtotime($date_to);
			
			if($stamp_date_from <= $stamp_date_to)
			{
				return TRUE;
			}else{
				return FALSE;
			}
		}else{
			return FALSE;
		}
	}
	
	public function _check_date($input_date = FALSE)
	{
		if(preg_match("/([0-9]{2})\/([0-9]{2})\/([0-9]{4})/", $input_date))
		{
			$d =explode('/',$input_date);
			if(count($d) > 3 OR count($d) <3)
			{
				return false;
			}else{
				if(checkdate($d[0],$d[1],$d[2]))
				{
					return TRUE;
				}else{
					return FALSE;
				}
			}
		}else{
			return FALSE;
		}
	}
} 
